home *** CD-ROM | disk | FTP | other *** search
/ CD Exchange / CD Exchange - Volume 1.iso / games / pd / chess / src.lha / src / main.c < prev    next >
C/C++ Source or Header  |  1992-09-06  |  9KB  |  402 lines

  1. /*
  2.  * main.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1988,1989,1990 John Stanback
  5.  * Copyright (c) 1992 Free Software Foundation
  6.  *
  7.  * This file is part of GNU CHESS.
  8.  *
  9.  * GNU Chess is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2, or (at your option)
  12.  * any later version.
  13.  *
  14.  * GNU Chess is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with GNU Chess; see the file COPYING.  If not, write to
  21.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24. #include "version.h"
  25. #include "gnuchess.h"
  26. #include <signal.h>
  27. char *ColorStr[2];
  28. char *CP[CPSIZE];
  29. /*
  30.  * In a networked enviroment gnuchess might be compiled on different hosts
  31.  * with different random number generators, that is not acceptable if they
  32.  * are going to share the same transposition table.
  33.  */
  34. unsigned long int next = 1;
  35.  
  36. unsigned int
  37. urand (void)
  38. {
  39.   next *= 1103515245;
  40.   next += 12345;
  41.   return ((unsigned int) (next >> 16) & 0xFFFF);
  42. }
  43.  
  44. void
  45. srand (unsigned int seed)
  46. {
  47.   next = seed;
  48. }
  49.  
  50. unsigned long hashkey, hashbd;
  51. struct hashval hashcode[2][7][64];
  52.  
  53. #ifdef ttblsz
  54. struct hashentry huge ttable[2][vttblsz + MAXrehash];
  55. unsigned int ttblsize;
  56. #endif
  57.  
  58.  
  59. char savefile[128] = "";
  60. char listfile[128] = "";
  61. #ifdef HISTORY
  62. unsigned char __far history[32768];
  63. #endif
  64. short rpthash[2][256];
  65. struct leaf Tree[TREE], *root;
  66. short TrPnt[MAXDEPTH];
  67. short PieceList[2][64], PawnCnt[2][8];
  68. short castld[2], Mvboard[64];
  69. short svalue[64];
  70. struct flags flag;
  71. short opponent, computer, WAwindow, WBwindow, BAwindow, BBwindow, dither, INCscore;
  72. long ResponseTime, ExtraTime, MaxResponseTime, et, et0, time0, ft;
  73. long NodeCnt, ETnodes, EvalNodes, HashCnt, HashAdd, FHashCnt, FHashAdd, HashCol,
  74.  THashCol, filesz;
  75. long replus, reminus;
  76. short HashDepth = HASHDEPTH, HashMoveLimit = HASHMOVELIMIT;
  77. short player, xwndw, rehash;
  78. struct GameRec __far GameList[MAXMOVES + MAXDEPTH];
  79. short Sdepth, GameCnt, Game50, MaxSearchDepth;
  80. short epsquare, contempt;
  81. int Book;
  82. struct TimeControlRec TimeControl;
  83. short TCflag, TCmoves, TCminutes, TCseconds, OperatorTime;
  84. short XCmoves[3], XCminutes[3], XCseconds[3], XC, XCmore;
  85. const short otherside[3] =
  86. {black, white, neutral};
  87. unsigned short hint;
  88. short int TOflag;        /* force search re-init if we backup search */
  89.  
  90. short mtl[2], pmtl[2], hung[2];
  91. short Pindex[64];
  92. short PieceCnt[2];
  93. short FROMsquare, TOsquare;
  94. short HasKnight[2], HasBishop[2], HasRook[2], HasQueen[2];
  95. short ChkFlag[MAXDEPTH], CptrFlag[MAXDEPTH], PawnThreat[MAXDEPTH];
  96. short Pscore[MAXDEPTH], Tscore[MAXDEPTH];
  97. const short qrook[3] =
  98. {0, 56, 0};
  99. const short krook[3] =
  100. {7, 63, 0};
  101. const short kingP[3] =
  102. {4, 60, 0};
  103. const short rank7[3] =
  104. {6, 1, 0};
  105. const short sweep[8] =
  106. {false, false, false, true, true, true, false, false};
  107. unsigned short killr0[MAXDEPTH], killr1[MAXDEPTH];
  108. unsigned short killr2[MAXDEPTH], killr3[MAXDEPTH];
  109. unsigned short PV, SwagHt, Swag0, Swag1, Swag2, Swag3, Swag4, sidebit;
  110. #ifdef KILLT
  111. short killt[0x4000];
  112. #endif
  113. const short value[7] =
  114. {0, valueP, valueN, valueB, valueR, valueQ, valueK};
  115. const short control[7] =
  116. {0, ctlP, ctlN, ctlB, ctlR, ctlQ, ctlK};
  117. short stage, stage2, Developed[2];
  118. FILE *hashfile;
  119. unsigned int starttime;
  120. short int ahead = true, hash = true;
  121. #ifdef AMIGA
  122. char *libdir;    /* path to GnuChess files */
  123. #endif
  124.  
  125.  
  126. #if defined CHESSTOOL || defined XBOARD
  127. void
  128. TerminateChess (int sig)
  129. {
  130.   ExitChess();
  131. }
  132. #endif
  133.  
  134. /* hmm.... shouldn`t main be moved to the interface routines */
  135. int
  136. main (int argc, char **argv)
  137. {
  138.   char *xwin = 0;
  139.   char *Lang = NULL;
  140.   srand (starttime = ((unsigned int) time ((long *) 0)));    /* init urand */
  141. #ifdef ttblsz
  142.   ttblsize = ttblsz;
  143.   rehash = -1;
  144. #endif /* ttblsz */
  145.   if (argc > 2)
  146.     {
  147.       if (argv[1][0] == '-' && argv[1][1] == 'L')
  148.     {
  149.       Lang = argv[2];
  150.       argv += 2;
  151.       argc -= 2;
  152.     }
  153.     }
  154. #ifdef AMIGA
  155. /*  if ((libdir = getenv("GNUCHESSDIR")) == NULL)*/
  156.     libdir = "misc/";
  157. #endif
  158.   InitConst (Lang);
  159.   ColorStr[0] = CP[118];
  160.   ColorStr[1] = CP[119];
  161.  
  162.   while (argc > 1 && ((argv[1][0] == '-') || (argv[1][0] == '+')))
  163.     {
  164.       switch (argv[1][1])
  165.     {
  166.     case 'a':
  167.       ahead = ((argv[1][0] == '-') ? false : true);
  168.       break;
  169.     case 'h':
  170.       hash = ((argv[1][0] == '-') ? false : true);
  171.       break;
  172.     case 's':
  173.       argc--;
  174.       argv++;
  175.       if (argc > 1)
  176.         strcpy (savefile, argv[1]);
  177.       break;
  178.     case 'l':
  179.       argc--;
  180.       argv++;
  181.       if (argc > 1)
  182.         strcpy (listfile, argv[1]);
  183.       break;
  184.  
  185. #if ttblsz
  186.     case 'r':
  187.       if (argc > 2)
  188.         rehash = atoi (argv[2]);
  189.       argc--;
  190.       argv++;
  191.       if (rehash > MAXrehash)
  192.         rehash = MAXrehash;
  193.       break;
  194.     case 'T':
  195.       if (argc > 2)
  196.         ttblsize = atoi (argv[2]);
  197.       argc--;
  198.       argv++;
  199.       if (ttblsize > 0 && ttblsize < 24)
  200.         ttblsize = (1 << ttblsize);
  201.       else
  202.         ttblsize = ttblsz;
  203.       break;
  204. #ifdef HASHFILE
  205.     case 't':        /* create or test persistent transposition
  206.                  * table */
  207.       hashfile = fopen (HASHFILE, RWA_ACC);
  208.       if (hashfile)
  209.         {
  210.           fseek (hashfile, 0L, SEEK_END);
  211.           filesz = (ftell (hashfile) / sizeof (struct fileentry)) - 1;
  212.         }
  213.       if (hashfile != NULL)
  214.         {
  215.           long i, j;
  216.           int nr[MAXDEPTH];
  217.           struct fileentry n;
  218.  
  219.           printf (CP[49]);
  220.           for (i = 0; i < MAXDEPTH; i++)
  221.         nr[i] = 0;
  222.           fseek (hashfile, 0L, SEEK_END);
  223.           i = ftell (hashfile) / sizeof (struct fileentry);
  224.           fseek (hashfile, 0L, SEEK_SET);
  225.           for (j = 0; j < i + 1; j++)
  226.         {
  227.           fread (&n, sizeof (struct fileentry), 1, hashfile);
  228.           if (n.depth)
  229.             {
  230.               nr[n.depth]++;
  231.               nr[0]++;
  232.             }
  233.         }
  234.           printf (CP[109],
  235.               nr[0], i);
  236.           for (j = 1; j < MAXDEPTH; j++)
  237.         printf ("%d ", nr[j]);
  238.           printf ("\n");
  239.         }
  240.       return 0;
  241.     case 'c':        /* create or test persistent transposition
  242.                  * table */
  243.       if (argc > 2)
  244.         filesz = atoi (argv[2]);
  245.       if (filesz > 0 && filesz < 24)
  246.         filesz = (1 << filesz) - 1 + MAXrehash;
  247.       else
  248.         filesz = Deffilesz + MAXrehash;
  249.       if ((hashfile = fopen (HASHFILE, RWA_ACC)) == NULL)
  250.         hashfile = fopen (HASHFILE, WA_ACC);
  251.       if (hashfile != NULL)
  252.         {
  253.           long j;
  254.           struct fileentry n;
  255.  
  256.           printf (CP[66]);
  257.           for (j = 0; j < 32; j++)
  258.         n.bd[j] = 0;
  259.           n.f = n.t = 0;
  260.           n.flags = 0;
  261.           n.depth = 0;
  262.           n.sh = n.sl = 0;
  263.           for (j = 0; j < filesz + 1; j++)
  264.         fwrite (&n, sizeof (struct fileentry), 1, hashfile);
  265.           fclose (hashfile);
  266.         }
  267.       else
  268.         printf (CP[50], HASHFILE);
  269.       return (0);
  270. #endif /* HASHFILE */
  271. #endif /* ttblsz */
  272.     case 'x':
  273.       xwin = &argv[1][2];
  274.       break;
  275.     case 'v':
  276.       fprintf (stderr, CP[102], version, patchlevel);
  277.       exit (1);
  278.     default:
  279.       fprintf (stderr, CP[113]);
  280.       exit (1);
  281.     }
  282.       argv++;
  283.       argc--;
  284.     }
  285.   XC = 0;
  286.   MaxResponseTime = 0;
  287. #if defined CHESSTOOL || defined XBOARD
  288.   signal (SIGTERM, TerminateChess);
  289.   TCflag = true;
  290.   TCmoves = 40;
  291.   TCminutes = 120;
  292.   TCseconds = 0;
  293.   OperatorTime = 0;
  294. #else
  295.   TCflag = false;
  296.   OperatorTime = 0;
  297. #endif
  298.   if (argc == 2)
  299.     {
  300.       char *p;
  301.  
  302.       MaxResponseTime = 100L*strtol(argv[1], &p, 10);
  303.       if (*p == ':')
  304.     MaxResponseTime = 60L*MaxResponseTime + 
  305.         100L*strtol(++p, (char **) NULL, 10);
  306.       TCflag = false;
  307.       TCmoves = 0;
  308.       TCminutes = 0;
  309.       TCseconds = 0;
  310.     }
  311.   if (argc >= 3)
  312.     {
  313.       char *p;
  314.       if (argc > 9)
  315.     {
  316.       fprintf (stderr, "%s\n", CP[220]);
  317.       exit (1);
  318.     }
  319.       TCmoves = atoi (argv[1]);
  320.       TCminutes = strtol (argv[2], &p, 10);
  321.       if (*p == ':')
  322.     TCseconds = strtol (p + 1, (char **) NULL, 10);
  323.       else
  324.     TCseconds = 0;
  325.       TCflag = true;
  326.       argc -= 3;
  327.       argv += 3;
  328.       while (argc > 1)
  329.     {
  330.       XCmoves[XC] = atoi (argv[0]);
  331.       XCminutes[XC] = strtol (argv[1], &p, 10);
  332.       if (*p == ':')
  333.         XCseconds[XC] = strtol (p + 1, (char **) NULL, 10);
  334.       else
  335.         XCseconds[XC] = 0;
  336.       if (XCmoves[XC] && (XCminutes[XC] || XCseconds[XC]))
  337.         XC++;
  338.       else
  339.         {
  340.           fprintf (stderr, CP